<?php

!defined('IN_APP') && exit('Access Denied');

class base {

    var $ip;
    var $time;
    var $db;
    var $cache;
    var $date;
    var $user = array();
    var $get = array();
    var $post = array();
    var $position = array();
    var $area = array();
    var $counter = array();
    var $statustable = array('0' => '未处理客户', '1' => '已咨询客户', '2' => '跟单提醒', '3' => '有意向客户', '4' => '储备客户', '5' => '签约客户', '6' => '出签客户', '9' => '跑单客户');
    var $week = array('0' => '星期天', '1' => '星期一', '2' => '星期二', '3' => '星期三', '4' => '星期四', '5' => '星期五', '星期六');

    function __construct(& $get, & $post) {
        $this->time = time();
        $this->ip = getip();
        $this->date = getdate($this->time);
        $this->get = & $get;
        $this->post = & $post;
        $this->init_db();
        $this->init_cache();
        $this->init_user();
        $this->init_counter();
    }

    function init_db() {
        $this->db = new db(DB_HOST, DB_USER, DB_PW, DB_NAME, DB_CHARSET, DB_CONNECT);
    }

    /* 一旦setting的缓存文件读取失败，则更新所有cache */

    function init_cache() {
        $this->cache = new cache($this->db);
        $this->position = $this->cache->load('position', 'id', 'displayorder');
        $this->area = $this->cache->load('area', 'id', 'displayorder');
    }

    /* 从缓存中读取数据，如果失败，则自动去读取数据然后写入缓存 */

    function fromcache($cachename, $cachetime=3) {
        $cachedata = $this->cache->read($cachename, intval($this->setting['index_life']) * 60); //首页数据缓存，时间为3秒
        if ($cachedata)
            return $cachedata;
        switch ($cachename) {
            case 'nosolvelist': //待解决问题，网友正在问
                #$this->load('question');
                #$cachedata = $_ENV['question']->list_by_cfield_cvalue_status('', 0, 1, 0, $this->setting['list_indexnosolve']);
                break;
            case 'solvelist'://已解决问题
                #$this->load('question');
                #$cachedata = $_ENV['question']->list_by_cfield_cvalue_status('', 0, 2, 0, $this->setting['list_indexnosolve']);
                break;
        }
        $this->cache->write($cachename, $cachedata);
        return $cachedata;
    }

    function load($model, $base = NULL) {
        $base = $base ? $base : $this;
        if (empty($_ENV[$model])) {
            require APP_ROOT . "/model/$model.class.php";
            eval('$_ENV[$model] = new ' . $model . 'model($base);');
        }
        return $_ENV[$model];
    }

    function init_user() {
        @$sid = tcookie('sid');
        @$auth = tcookie('auth');
        $user = array();
        @list($uid, $password) = empty($auth) ? array(0, 0) : taddslashes(explode("\t", strcode($auth, AUTH_KEY, 'DECODE')), 1);
        if (!$sid) {
            $sid = random(6);
            tcookie('sid', $sid, 31536000);
        }
        if ($uid && $password) {
            $this->load('user');
            $user = $_ENV['user']->get($uid);
            ($password != $user['password']) && $user = array();
        }
        if (!$user) {
            $user['uid'] = 0;
        }
        $user['sid'] = $sid;
        $this->user = $user;
        //用户头像初始化
        // $user['uid'] && $user['avatar'] = get_avatar_dir($user['uid']);
    }

    /* 全局统计 */

    function init_counter() {
        $daystart = strtotime($this->date['year'] . '-' . $this->date['mon'] . '-' . $this->date['mday'] . ' 00:00:00');
        $dayend = strtotime($this->date['year'] . '-' . $this->date['mon'] . '-' . $this->date['mday'] . ' 23:59:59');
        //今天
        $counter['today'] = $this->db->fetch_total("customer", " time>$daystart AND time<$dayend");
        //昨天
        $counter['yesterday'] = $this->db->fetch_total("customer", " time>" . ($timestart - 24 * 3600) . " AND time<" . ($timeend - 24 * 3600));
        //本月
        $monstart = strtotime($this->date['year'] . '-' . $this->date['mon'] . '-1 00:00:00');
        $monend = strtotime($this->date['year'] . '-' . $this->date['mon'] . '-30 23:59:59');
        $counter['month'] = $this->db->fetch_total("customer", " time>" . $monstart . " AND time<" . $monend);
        //所有
        $counter['total'] = $this->db->fetch_total("customer", " 1=1");
        $this->counter = $counter;
    }

    /* 权限检测 */

    function checkable($url) {
        return true;
    }

    /* 	中转提示页面
      $ishtml=1 表示是跳转到静态网页
     */

    function message($message, $url = '') {
        $seotitle = '操作提示';
        if ('' == $url) {
            $redirect = SITE_URL;
        } else if ('BACK' == $url) {
            $redirect = $url;
        } else {
            $redirect = SITE_URL . $this->setting['seo_prefix'] . $url . $this->setting['seo_suffix'];
        }
        $tpldir = (0 === strpos($this->get[0], 'admin')) ? 'admin' : $this->setting['tpl_dir'];
        include template('tip', $tpldir);
        exit;
    }

    /* 检查验证码 */

    function checkcode() {
        $this->load('user');
        if (strtolower(trim($this->post['code'])) != $_ENV['user']->get_code()) {
            $this->message("验证码错误!", 'BACK');
        }
    }

}

?>
